package com.saint.farmshop.Service.ServiceImpl;

import com.saint.farmshop.Dto.OrderDto;
import com.saint.farmshop.Entity.Order;
import com.saint.farmshop.Entity.User;
import com.saint.farmshop.Mapper.MapperImpl.OrderMapperImpl;
import com.saint.farmshop.Mapper.MapperImpl.ProductionMapperImpl;
import com.saint.farmshop.Mapper.MapperImpl.UserMapperImpl;
import com.saint.farmshop.Service.OrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 订单服务实现类
 *
 * @author Saint
 * @date 2022/06/30
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    OrderMapperImpl orderMapper;

    @Resource
    ProductionMapperImpl productionMapper;

    @Resource
    UserMapperImpl userMapper;

    @Override
    public OrderDto createOrder(Order order) {
        order.setUpdateTime(LocalDateTime.now());
        if(orderMapper.createOrder(order) == true){
            productionMapper.updateProductionStatus(order.getProductionId());
            return orderToDto(order);
        }
        else{
            return null;
        }
    }

    @Override
    public List<OrderDto> getOrderList(String openId) {
        List<Order> orderList = orderMapper.getOrderList(openId);
        List<OrderDto> orderDtoList = new ArrayList<>();
        for (Order order : orderList) {
            orderDtoList.add(orderToDto(order));
        }
        return orderDtoList;
    }

    private OrderDto orderToDto(Order order){
        OrderDto orderDto = new OrderDto();
        orderDto.setOrder(order);
        orderDto.setShop(userMapper.selectUserByOpenId(order.getUserId()));
        orderDto.setProductionDetail(productionMapper.getProductionDetailById(order.getProductionId()));
        return orderDto;
    }
}
